package com.weibotwo.web.controller;

import com.weibotwo.common.Exception.UserServiceException;
import com.weibotwo.common.util.PageResult;
import com.weibotwo.pojo.User;
import com.weibotwo.service.AdminService;
import com.weibotwo.service.WeiBoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 管理员表现层
 * @ClassName AdminController
 * @Author 天火
 * @Date 2019-03-26
 * @Version 1.0

 */
@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private WeiBoService weiBoService;

    private ModelAndView modelAndView = new ModelAndView();

    /**
     * 管理员登录
     * @param name
     * @param password
     * @param session
     * @return
     */
    @RequestMapping("adminLogin")
    public ModelAndView adminlogin(String name, String password, HttpSession session){
        modelAndView.clear();

        try {
            User adminUser = adminService.adminLogin(name,password);
            session.setAttribute("admin", adminUser);
            modelAndView.setViewName("redirect:adminIndex");
        } catch (UserServiceException e) {
            modelAndView.setViewName("admin/adminlogin");
            modelAndView.addObject("msg", "用户名或密码错误！");
            return modelAndView;
        }
        return modelAndView;
    }


    /**
     * 管理主页
     * @return ModelAndView
     */
    @RequestMapping("adminIndex")
    public ModelAndView adminIndex(HttpSession session){
        //注入服务
        modelAndView.clear();
        User user = (User) session.getAttribute("admin");
        if(user ==null){
            modelAndView.setViewName("redirect:admin_login");
            return modelAndView;
        }
        modelAndView.setViewName("admin/index");
        //首页展示数据

        try {
            Map<String, Object> map = adminService.showAdminIndex();
            modelAndView.addObject("map", map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelAndView;
    }


    /**
     * 用户管理
     * @param pageNum 页数
     * @param key 关键词
     * @return ModelAndView
     */
    @RequestMapping("userManage")
    public ModelAndView userManage(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, String key,HttpSession session){
        modelAndView.clear();
        User user = (User) session.getAttribute("admin");
        if(user ==null){
            modelAndView.setViewName("redirect:admin_login");
            return modelAndView;
        }
        modelAndView.setViewName("admin/user");
        String ukey = "";
        PageResult result;
        if("all".equals(key)){
            ukey = null;
        }else{
            ukey = key;
        }
        try {
            if(ukey == null){
                result =  adminService.findAllUser(pageNum);
            }else{
                result = adminService.findAllUserByKey(ukey,pageNum);

            }
            modelAndView.addObject("userList", result.getRows());
            modelAndView.addObject("pageNum",pageNum);
            modelAndView.addObject("totalPage",result.getPageTotals());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return modelAndView;
    }

    /**
     * 微博管理
     * @param pageNum 页数
     * @param key 关键词
     * @return ModelAndView
     */
    @RequestMapping("weiboManage")
    public ModelAndView weiboManage(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, String key,HttpSession session){
        modelAndView.clear();
        User user = (User) session.getAttribute("admin");
        if(user ==null){
            modelAndView.setViewName("redirect:admin_login");
            return modelAndView;
        }
        modelAndView.setViewName("admin/weibo");
        String wkey = "";
        PageResult result ;
        try {
            if("all".equals(key)||key==null){
                wkey = null;
                result= weiBoService.findPageById(pageNum,10);
            }else{
                wkey = key;
                result = weiBoService.seachWeibopojoList(wkey,pageNum,"weibo");
            }
            modelAndView.addObject("weiboList",result.getRows());
            modelAndView.addObject("pageNum",pageNum);
            modelAndView.addObject("totalPage",result.getPageTotals());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelAndView;
    }

    /**
     * 微博操作
     * @param wId
     * @param ac
     * @return
     */
    @RequestMapping("updateWeibo")
    public ModelAndView upWeibo(long wId,String ac){
        modelAndView.clear();
        modelAndView.setViewName("redirect:weiboManage");
        try {
            adminService.updateWeiboState(wId,ac);
            modelAndView.addObject("pageNum", 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return modelAndView;
    }

    /**
     * 贝壳管理
     * @param pageNum 页数
     * @param key 搜索词
     * @return ModelAndView
     */
    @RequestMapping("shellManage")
    public ModelAndView conchManage(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, String key,HttpSession session){
        modelAndView.clear();
        User user = (User) session.getAttribute("admin");
        if(user ==null){
            modelAndView.setViewName("redirect:admin_login");
            return modelAndView;
        }
        modelAndView.setViewName("admin/shell");
        String ckey = "";
        if("all".equals(key)){
            ckey = null;
        }else{
            ckey = key;
        }
        try {

            PageResult result = adminService.findAllShellNyKey(ckey,pageNum);
            modelAndView.addObject("shellList", result.getRows());
            modelAndView.addObject("pageNum",pageNum);
            modelAndView.addObject("totalPage",result.getPageTotals());
        } catch (Exception e) {
            e.printStackTrace();
        }
       
        return modelAndView;
    }

    @RequestMapping("/bannedUser")
    public ModelAndView fjUser(long uId,int day){
        modelAndView.clear();
        try {
            adminService.bannedUserDaysById(uId,day);	//uId封禁用户编号，day封禁天数
            modelAndView.setViewName("redirect:userManage");
            modelAndView.addObject("pageNum",1);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return modelAndView;
    }

    @RequestMapping("unBlockUser")
    public ModelAndView unBlockUser(Long uId){
        //注入服务
        //调用方法
        modelAndView.clear();
        try{
            adminService.unBlockUserByuerId(uId);
            modelAndView.setViewName("redirect:userManage");
            modelAndView.addObject("pageNum",1);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/updateShell")
    public ModelAndView updateShell(Long sId,String ac){
        modelAndView.clear();
        modelAndView.setViewName("redirect:shellManage");
        try{
            adminService.deleteAndRestoreShell(sId,ac);
        }catch (Exception e){
            e.printStackTrace();
        }
        return modelAndView;
    }

    @RequestMapping("/adminExit")
    private String exit(){
        return "admin/adminlogin";
    }




}
